// 全局共享数据示例
import {DEFAULT_NAME} from '@/constants';
import request from '@/utils/request'
import {history, Link} from 'umi';
import {useState} from 'react';
import {useModel} from "@@/exports";

const useUser = () => {
    const token = sessionStorage.getItem('token');
    // @ts-ignore
    const userInfoInSession = sessionStorage.getItem('userInfo') ? JSON.parse(sessionStorage.getItem('userInfo')) : {};
    const [name, setName] = useState<string>(DEFAULT_NAME);
    let userInfo: any = {};
    if (!token) {
        history.push('/login');
        return;
    }
    request('/api2/auth/userInfo', {
        method: 'GET',
        params: {
           id: userInfoInSession.id || ''
        },
    }).then((res) => {
        const {code} = res;
        if (code && code === 200) {
            // userInfo = {
            //     ...res.userInfo
            // }
            for (const d in  res.userInfo){
                userInfo[d] = res.userInfo[d]
            }
            userInfo.name = '演示帐号';
            userInfo.avatar =
                'https://jgy-1313248182.cos.ap-nanjing.myqcloud.com/logo.png';
            userInfo.phone = '188****6666';
        }
    });
    return {
        name,
        setName,
        userInfo,
    };
};

export default useUser;
